<template>
  <div class="container">
    <div class="app-container">
      <el-tree
        default-expand-all
        show-checkbox
        node-key="id"
        :data="depts"
        :props="defaultProps"
      >
        <template v-slot="{ data }">
          <el-row
            type="flex"
            align="middle"
            justify="space-between"
            style="width: 100%; height: 40px"
          >
            <el-col :span="16">{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{
                data.managerName ? "1" : "2"
              }}</span>
              <el-dropdown @command="operateDept($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>

                  <el-dropdown-item command="edit"> 编辑部门</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>

      <!-- 弹层代码 -->
      <!-- <el-button type="text" @click="dialogFormVisible = true">打开嵌套表单的 Dialog</el-button> -->
      <add-dept :showDialog.sync="showDialog" ></add-dept>
    </div>
    
  </div>
</template>
<script>
import { getDepartment } from "@/api/department";
import { transListToTreeData } from "@/utils";
import AddDept from './components/add-dept.vue';
export default {
  components: { AddDept },

  name: "Department",
  data() {
    return {
      depts: [],
      defaultProps: {
        children: "children", 
        label: "name",
      },
      // 控制弹层显示
      showDialog: false,
      // 存储当前点击的节点id
      currentNodeId:null
    };
  },
  created() {
    this.getDepartment();
  },
  methods: {
    async getDepartment() {
      const data = await getDepartment();
      this.depts = transListToTreeData(data.data, 0);
      console.log("@@", this.depts);
    },

    operateDept(select,id) {
      if (select === "add") {
        this.showDialog = true;
        //  设置当前节点
        this.currentNodeId = id
      }
    },
  },
};
</script>
<style scoped >
.app-container {
  padding: 30px 140px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
.el-dropdown-menu__item:hover {
  /* 去掉背景色 */
  background-color: transparent !important;
}
</style>
